/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */ 
package de.rockon.fuzzy.controller.gui.tree.treehelper;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreePath;

import de.rockon.fuzzy.controller.model.FuzzyBasicElement;
import de.rockon.fuzzy.controller.model.FuzzyController;
import de.rockon.fuzzy.controller.model.FuzzyPoint;

/**
 * Das Datenmodel des FuzzyTree
 */
public class RockOnTreeModel implements javax.swing.tree.TreeModel {
	private final List<TreeModelListener> listeners = new ArrayList<TreeModelListener>();

	@Override
	public void addTreeModelListener(final TreeModelListener l) {
		listeners.add(l);
	}

	@Override
	public Object getChild(final Object parent, final int index) {
		final FuzzyBasicElement fs = (FuzzyBasicElement) parent;
		return fs.getChild(index);
	}

	@Override
	public int getChildCount(final Object parent) {
		final FuzzyBasicElement fs = (FuzzyBasicElement) parent;
		return fs.size();
	}

	@Override
	public int getIndexOfChild(final Object parent, final Object child) {
		final FuzzyBasicElement fs = (FuzzyBasicElement) parent;
		return fs.getIndexOfChild((FuzzyBasicElement) child);
	}

	@Override
	public Object getRoot() {
		return FuzzyController.getInstance();
	}

	@Override
	public boolean isLeaf(final Object node) {
		if (node instanceof FuzzyPoint) {
			return true;
		}
		return false;
	}

	@Override
	public void removeTreeModelListener(final TreeModelListener l) {
		listeners.remove(l);
	}

	@Override
	public void valueForPathChanged(final TreePath path, final Object newValue) {
		if (path.getLastPathComponent() == null) {
			return;
		}

		final FuzzyBasicElement fs = (FuzzyBasicElement<?, ?>) path.getLastPathComponent();
		fs.editValue(newValue);
	}
}